<?php
namespace App\Domain;
use App\Model\Photo as Model_Photo;
use App\Domain\Vip as Domain_Vip;

class Photo {

    /* 我的照片 */
	public function myPhoto($uid,$status,$p) {

        $model = new Model_Photo();
        $list= $model->myPhoto($uid,$status,$p);
        
        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['views']=\App\NumberFormat($v['views']);
            if($v['status']<2){
                $v['reason']='';
            }
            
            $list[$k]=$v;
        }
        
		return $list;
	}

    /* 照片价格 */
	public function getFee() {

        $key='photo_fee';
		$list=\App\getcaches($key);
		if(!$list){
            $model = new Model_Photo();
			$list= $model->getFee();
            if($list){
                \App\setcaches($key,$list);
            }
		}

		return $list;
	}

    /* 发布照片 */
	public function setPhoto($data) {
		$rs = array('code' => 0, 'msg' => \PhalApi\T('发布成功'), 'info' => array());
        
        $uid=$data['uid'];
        $thumb=$data['thumb'];

//        $isauth=\App\isAuthorAuth($uid);
//        if(!$isauth){
//            $rs['code']=1003;
//			$rs['msg']=\PhalApi\T('您还未认证主播或主播认证还未通过');
//			return $rs;
//        }
        
        $nowtime=time();
        if($data['isprivate']==1 && $data['coin']<=0){
            $rs['code']=1005;
			$rs['msg']=\PhalApi\T('请选择价格');
			return $rs;
        }
        
        if($data['isprivate']==0){
            $data['coin']='0';
        }
        $data['addtime']=$nowtime;
        $data['uptime']=$nowtime;
        
		$model = new Model_Photo();
        $result = $model->setPhoto($data);

        if(!$result){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('发布失败，请重试');
			return $rs;
        }
        
		return $rs;
	}

    /* 设为公开 */
	public function setPublic($uid,$photoid) {
        
        $data=[
            'isprivate'=>'0',
            'coin'=>'0',
        ];
        
		$model = new Model_Photo();
        $result = $model->upPhoto($uid,$photoid,$data);

		return $result;
	}

    /* 观看 */
	public function addView($uid,$photoid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());

		$model = new Model_Photo();
        
        $info = $model->getPhoto($photoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('照片不存在');
			return $rs;
        }
        
        if($info['uid']==$uid){
            
            $nums=$info['views'];
            $rs['info'][0]['nums']=\App\NumberFormat($nums);
            return $rs;
        }
        
        $model->addView($uid,$photoid);

        $nums=$info['views']+1;
        
        $rs['info'][0]['nums']=\App\NumberFormat($nums);

		return $rs;
	}


    /* 删除 */
	public function delPhoto($uid,$photoid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
		$model = new Model_Photo();
        $where="uid={$uid}";
        $info = $model->getPhoto($photoid,$where);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('照片不存在');
			return $rs;
        }
        
        if($info['uid']!=$uid){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('操作失败');
			return $rs;
        }
        
        $result = $model->delPhoto($photoid);

        if(!$result){
            $rs['code']=1005;
            $rs['msg']=\PhalApi\T('操作失败');
            return $rs;
        }

		return $rs;
	}

    /* 个人照片 */
	public function getHomePhoto($uid,$liveuid,$p) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
		$model = new Model_Photo();
        
        $domain_vip = new Domain_Vip();
        
        $isvip=0;
        $vipinfo=$domain_vip->myVip($uid);
        if($vipinfo['isvip']){
            $isvip=1;
        }
        
        $where=[];
        $where['uid']=$liveuid;
        
        $list = $model->getPhotoList($where,$p);
        
        if(!$list){
            $list=[];
        }

        if(!empty($list)){
            foreach($list as $k=>$v){
                $v['thumb']=\App\get_upload_path($v['thumb']);
                $v['views']=\App\NumberFormat($v['views']);
                $v['cansee']='0';
                if($v['uid']==$uid){
                    $v['cansee']='1';
                }else{
                    if($v['isprivate']==1){
                        $isbuy=$model->isbuy($uid,$v['id']);
                        if($isbuy || $isvip){
                            $v['cansee']='1';
                        }
                    }else{
                        $v['cansee']='1';
                    }
                }
                $list[$k]=$v;
            }
        }
        

        $rs['info']=$list;

		return $rs;

	}

    /* 付费 */
	public function buyPhoto($uid,$photoid) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
        $model = new Model_Photo();
        
        $info = $model->getPhoto($photoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('照片不存在');
			return $rs;
        }
        
        if($info['isprivate']==0){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('该照片不需要付费');
			return $rs;
        }
        
        //$href=get_upload_path($info['href']);
        if($info['uid']==$uid){
            //$rs['info'][0]['href']=$href;
            return $rs;
        }
        $total=$info['coin'];
        
        if($total==0){
            //$rs['info'][0]['href']=$href;
            return $rs;
        }
        
        $isbuy=$model->isbuy($uid,$info['id']);
        if($isbuy){
            //$rs['info'][0]['href']=$href;
            return $rs;
        }
        
        $addtime=time();
		$type='0';
		$action='5';
        
		/* 更新用户余额 消费 */
		$ifok =\App\upCoin($uid,$total);
        if(!$ifok){
            $rs['code'] = 1005;
			$rs['msg'] = \PhalApi\T('余额不足');
			return $rs;
        }

		/* 更新直播 魅力值 累计魅力值 */
		\App\addVotes($info['uid'],$total,$total);

		$insert=array(
            "type"=>$type,
            "action"=>$action,
            "uid"=>$uid,
            "touid"=>$info['uid'],
            "actionid"=>$info['id'],
            "nums"=>1,
            "totalcoin"=>$total,
            "addtime"=>$addtime 
        );
        
        \App\addCoinRecord($insert);

        //增加映票记录
        $data=array(
            'type'=>1,
            'action'=>7, //照片收费
            'uid'=>$info['uid'],
            'fromid'=>$uid,
            'actionid'=>$info['id'],
            'nums'=>1,
            'total'=>$total,
            'showid'=>0,
            'votes'=>$total,
            'addtime'=>$addtime
        );

        \App\addVoteRecord($data);

        $data=[
            'uid'=>$uid,
            'photoid'=>$info['id'],
            'addtime'=>$addtime,
        ];
        $model->buyPhoto($data);

        //$rs['info'][0]['href']=$href;

		return $rs;

	}
	
}
